/**
 * 
 */
package fr.flogo.destinee.cameras;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.util.FloatMath;

import fr.flogo.destinee.positions.Vector3D;

/**
 * @author Florian GOLESTIN
 * TODO Mettre gl dans le contructeur
 */
public class VerySimpleCamera implements ICamera {

	private static final float M_PI = 3.14159265f;
	private float[] position = new float[] { 0.0f, 0.0f, 0.0f};
	private float angle;
	private float[] rotation = new float[] { 0.0f, 0.0f, 0.0f};
	
	
	private float  _phi = 0;
	private Vector3D _forward = new Vector3D(0.0, 0.0, 0.0);
	private float _theta = 0;
	private Vector3D _left = new Vector3D(0.0f, 0.0f, 0.0f);
	private Vector3D _target = new Vector3D(0.0f, 0.0f, 0.0f);
	private Vector3D _position = new Vector3D(0.0f, 0.0f, 0.0f);
	
	public VerySimpleCamera() {
	}
	
	/* (non-Javadoc)
	 * @see fr.flogo.destinee.cameras.ICamera#translate(GL10 gl, float, float, float)
	 */
	@Override
	public void translate(GL10 gl, float x, float y, float z) {
//		gl.glTranslatef(x, y, z);
//		this.position[0] = x;
//		this.position[1] = y;
//		this.position[2] = z;
		// TODO enum x = 0, y = 1, z = 2
		
		this._position = new Vector3D(x, y, z);
		vectorsFromAngle();
	}
	
	public void move(float x, float y, float z) {
//		this.position[0] += x;
//		this.position[1] += y;
//		this.position[2] += z;
		
		_position.move(_forward.multiply(z * 10));
		
		_target = new Vector3D(_position); 
		_target.add(_forward);
	}
	
	public void turn(float angle, float x, float y, float z) {
		
//		this.rotation[0] += x;
//		this.rotation[1] += y;
//		this.rotation[2] += z;
//		this.angle += angle;
		
		
		System.out.println("Y = "+y);
		this._theta -= angle*0.02;
//		this._phi -= angle*0.02;
		this.vectorsFromAngle();
	}

	/* (non-Javadoc)
	 * @see fr.flogo.destinee.cameras.ICamera#rotation(GL10gl,float, float, float, float)
	 */
	@Override
	public void rotation(GL10 gl, float angle, float x, float y, float z) {
//		gl.glRotatef(angle, x, y, z);
//		this.rotation[0] = x;
//		this.rotation[1] = y;
//		this.rotation[2] = z;
//		this.angle = angle;
		
		
		
	}

	/* (non-Javadoc)
	 * @see fr.flogo.destinee.cameras.ICamera#getPosition(java.lang.Float, java.lang.Float, java.lang.Float)
	 */
	@Override
	public float[] getPosition() {
		return this.position;
	}

//	public void drawMovement(GL10 gl) {
//		float xtrans = -this.position[0];						//Used For Player Translation On The X Axis
//		float ztrans = -this.position[2];						//Used For Player Translation On The Z Axis
////		float ytrans = -walkbias - 0.25f;			//Used For Bouncing Motion Up And Down
//		float sceneroty = 360.0f - this.angle;
//		
//		//View
////				gl.glRotatef(lookupdown, 1.0f, 0, 0);		//Rotate Up And Down To Look Up And Down
////				gl.glRotatef(sceneroty, 0, 1.0f, 0);		//Rotate Depending On Direction Player Is Facing
////				gl.glTranslatef(xtrans, /*ytrans*/0.0f, ztrans);	//Translate The Scene Based On Player Position
//		
//		this.rotation(gl, this.angle, this.rotation[0], this.rotation[1], this.rotation[2]);
//		this.translate(gl, this.position[0], this.position[1], this.position[2]);
//		
//		// TODO plusieur rotations possible ?
//	}

	public void drawMovement(GL10 gl) {
		GLU.gluLookAt(gl, _position.x(),_position.y(),_position.z(),
	              _target.x(),_target.y(),_target.z(),
	              0,0,1);
	}
	
	
	public  void vectorsFromAngle() {
		Vector3D up = new Vector3D(0.0f,1.0f,0.0f); //une constante, le vecteur vertical du monde, utilisé dans les calculs
		
		//On limite les valeurs de _phi, on vole certes, mais on en fait pas de loopings :p
		    if (_phi > 89)
		        _phi = 89;
		    else if (_phi < -89)
		        _phi = -89;
		//passage des coordonnées sphériques aux coordonnées cartésiennes
		    double r_temp = FloatMath.cos(_phi*M_PI/180);
		    _forward.setZ(FloatMath.sin(_phi*M_PI/180));
		    _forward.setX(r_temp*FloatMath.cos(_theta*M_PI/180));
		    _forward.setY(r_temp*FloatMath.sin(_theta*M_PI/180));
		//diantre mais que fait ce passage ?
		    _left  = up.crossProduct(_forward);
		    _left.normalize();

		//avec la position de la caméra et la direction du regard, on calcule facilement ce que regarde la caméra (la cible)
		    _target = _position.add(_forward);
	}
	
	
//	public void look(GL10 gl) {
		
//	}
}
